﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Qreed.Xna.Font
{
    public abstract class RenderFont : VectorFont
    {
        /// <summary>
        /// Loads the content.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device.</param>
        /// <param name="basedOnFont">The based on font.</param>
        public virtual void LoadContent(GraphicsDevice graphicsDevice, VectorFont basedOnFont)
        {
            this.FontName = basedOnFont.FontName;
            this.KerningTable = basedOnFont.KerningTable;
            this.LineHeight = basedOnFont.LineHeight;

            _graphicsDevice = graphicsDevice;
            _vertexDeclaration = new VertexDeclaration(graphicsDevice, VertexPositionNormalTexture.VertexElements);

            foreach (char c in basedOnFont.CharacterMap.Keys)
            {
                VectorFontCharacter basedOnCharacter = (VectorFontCharacter)basedOnFont[c];
                VectorFontCharacterMesh characterMesh = CreateNewMeshItem();

                characterMesh.LoadContent(basedOnCharacter);

                CharacterMap.Add(c, characterMesh);
            }
        }

        /// <summary>
        /// Unloads the content.
        /// </summary>
        public virtual void UnloadContent()
        {
            if (_vertexDeclaration != null)
            {
                _vertexDeclaration.Dispose();
                _vertexDeclaration = null;
            }

            foreach (char c in CharacterMap.Keys)
            {
                ((VectorFontCharacterMesh)CharacterMap[c]).UnloadContent();
            }

            CharacterMap.Clear();

            _graphicsDevice = null;
        }

        /// <summary>
        /// Creates a new mesh item.
        /// </summary>
        /// <returns></returns>
        protected abstract VectorFontCharacterMesh CreateNewMeshItem();

        private GraphicsDevice _graphicsDevice;
        /// <summary>
        /// Gets or sets the graphics device.
        /// </summary>
        /// <value>The graphics device.</value>
        public GraphicsDevice GraphicsDevice
        {
            get { return this._graphicsDevice; }
        }

        private VertexDeclaration _vertexDeclaration;
        /// <summary>
        /// Gets or sets the vertex declaration.
        /// </summary>
        /// <value>The vertex declaration.</value>
        public VertexDeclaration VertexDeclaration
        {
            get { return this._vertexDeclaration; }
        }	
    }
}
